#!/usr/bin/env python3
import sys, os
import argparse
import yaml  # pip install pyyaml
import logging
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')

DATASET_ZOO = os.path.abspath('datasets'); assert os.path.isdir(DATASET_ZOO)

if __name__ == '__main__':

    if not os.path.isfile('utils/oss'):
        logging.error("Please install oss via utils/install_aliyun_oss_client.sh")
        sys.exit(-1)

    if not os.path.isfile('credentials/aliyun_oss.cfg'):
        instructions_to_get_credential = (
            'You need credential to use the leaderboard:\n  '
            'Please send email with title "oss.cfg" to leaderboard@speechio.ai, '
            'and paste replied content to credentials/aliyun_oss.cfg'
        )
        logging.error(instructions_to_get_credential)
        sys.exit(-1)

    with open(os.path.join(DATASET_ZOO, 'zoo.yaml'), 'r', encoding='utf8') as f:
        remote_dataset_zoo = yaml.safe_load(f)
        for dataset_id in remote_dataset_zoo.keys():
            src = remote_dataset_zoo[dataset_id]['url']
            dst = os.path.join(DATASET_ZOO, dataset_id)
            cmd = F'utils/oss -c credentials/aliyun_oss.cfg cp -ur {src} {dst}'
            logging.info(cmd)
            os.system(cmd)
